package com.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.ConvenienceDao_Rising_Prices;
import com.dao.RisingPricesHistoryDao;
import com.dao.SysUserDao;
import com.pojo.RisingPrices;
import com.pojo.RisingPricesHistory;
import com.pojo.TopUp;
import com.service.ConvenienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ConvenienceServiceImpl implements ConvenienceService {

    /*水电气历史充值操作*/
    @Autowired
    private RisingPricesHistoryDao risingPricesHistoryDao;

    /*水电气的数据库操作*/
    @Autowired
    private ConvenienceDao_Rising_Prices cd_r_p;

    /*用户的金额操作*/
    @Autowired
    private SysUserDao sysUserDao;

    /*可以通过用户ID来查询水电气的金额*/
    @Override
    public RisingPrices find(int id) {
        System.out.println("Rising_Prices进来了....");

        RisingPrices rising_prices = cd_r_p.selectById(id);

        System.out.println("正在输出");
        System.out.println(rising_prices);

        return rising_prices;
    }

    /*暂时通过1代表水费。2代表电费。3代表气费  这是加钱,topUp代表充值的金额，用户的信息，充值的类别*/
    @Override
    public void Rising_Prices_addChargeMoney(TopUp topUp) {

        System.out.println("Rising_Prices_ChargeMoney进来了....");

        RisingPrices risingPrices = new RisingPrices();

        /*获取当前时间*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        topUp.setDate(date);

        /*设置充值历史的参数*/
        RisingPricesHistory history=new RisingPricesHistory();
        history.setId(topUp.getId());
        history.setDate(topUp.getDate());
        history.setName(topUp.getName());
        history.setAddress(topUp.getAddress());
        history.setName(topUp.getName());

        /*判断充值哪一类*/
        switch (topUp.getCategory()){
            case 1:{
                System.out.println("执行了水费充值以及扣除用户的金钱，以及插入一条记录");
                risingPrices.setId(topUp.getId());
                risingPrices.setWater(topUp.getMoney());
                /*用来增加水费*/
                cd_r_p.waterAddChargeMoney(risingPrices);

                /*用来扣除用户的金钱*/
                sysUserDao.Deduct(topUp.getId(),topUp.getMoney());

                /*获取充值之后的水费*/
                RisingPrices risingPrices1 = cd_r_p.selectById(topUp.getId());
                Integer water = risingPrices.getWater();
                System.out.println("判断是否增加了水费"+water);
                /*设置总共的金额*/
                history.setTotal(water);
                /*设置了水费充值金额*/
                history.setWater(topUp.getMoney());
                /*添加一条就记录到里面*/
                risingPricesHistoryDao.insert(history);
                System.out.println("执行了水费充值以及扣除用户的金钱，以及插入一条记录");

                break;
            }
            case 2:{
                System.out.println("执行了电费充值以及扣除用户的金钱，以及插入一条记录");
                risingPrices.setId(topUp.getId());
                risingPrices.setElectric(topUp.getMoney());
                /*用来增加电费*/
                cd_r_p.electricAddChargeMoney(risingPrices);

                /*用来扣除用户的金钱*/
                sysUserDao.Deduct(topUp.getId(),topUp.getMoney());

                /*获取充值之后的水费*/
                RisingPrices risingPrices1 = cd_r_p.selectById(topUp.getId());
                Integer electric = risingPrices.getElectric();
                System.out.println("判断是否增加了费电费"+electric);
                /*设置总共的金额*/
                history.setTotal(electric);
                /*在历史记录里，设置了电费充值金额*/
                history.setElectric(topUp.getMoney());
                /*添加一条就记录到里面*/
                risingPricesHistoryDao.insert(history);
                System.out.println("执行了电费充值以及扣除用户的金钱，以及插入一条记录");
                break;
            }
            case 3:{
                System.out.println("执行了天然气费用充值以及扣除用户的金钱，以及插入一条记录");
                risingPrices.setId(topUp.getId());
                risingPrices.setGas(topUp.getMoney());
                /*用来增加天然气费*/
                cd_r_p.gasAddChargeMoney(risingPrices);

                /*用来扣除用户的金钱*/
                sysUserDao.Deduct(topUp.getId(),topUp.getMoney());

                /*获取充值之后的水费*/
                RisingPrices risingPrices1 = cd_r_p.selectById(topUp.getId());
                Integer gas = risingPrices.getGas();
                System.out.println("判断是否增加了天然气费"+gas);
                /*设置总共的金额*/
                history.setTotal(gas);
                /*设置了天然气费充值金额*/
                history.setGas(topUp.getMoney());
                /*添加一条就记录到历史记录里面*/
                risingPricesHistoryDao.insert(history);
                System.out.println("执行了天然气费充值以及扣除用户的金钱，以及插入一条记录");
                break;
            }
            default:{

                System.out.println("执行了default");
                System.out.println("哥们别输错了");
            }

        }
    }

    /*暂时通过1代表水费。2代表电费。3代表气费  这是扣钱*/
    @Override
    public void Rising_Prices_descendChargeMoney(int id, int category, int money) {

        System.out.println("Rising_Prices_ChargeMoney进来了....");

        RisingPrices risingPrices = new RisingPrices();

        switch (category){
            case 1:{
                System.out.println("执行了水费充值");
                risingPrices.setId(id);
                risingPrices.setWater(money);
                cd_r_p.waterDescendChargeMoney(risingPrices);
                break;
            }
            case 2:{
                System.out.println("执行了电费充值");

                risingPrices.setId(id);
                risingPrices.setElectric(money);
                cd_r_p.electricDescendChargeMoney(risingPrices);
                break;
            }
            case 3:{
                System.out.println("执行了天然气充值");

                risingPrices.setId(id);
                risingPrices.setGas(money);
                cd_r_p.gasDescendChargeMoney(risingPrices);
                break;
            }
            default:{

                System.out.println("执行了default");
                System.out.println("哥们别输错了");
            }

        }

    }


    /*通过id，以及分类来查询充值历史记录*/
    @Override
    public List<RisingPricesHistory> RisingPricesHistoryFind(int id,int category) {

        List<RisingPricesHistory> list=null;
        switch (category){
            case 1:{
                System.out.println("执行水费充值历史的查询");
                QueryWrapper<RisingPricesHistory> wrapper = new QueryWrapper<>();
                wrapper.eq("id","1");
                wrapper.ne("water","null");
                list = risingPricesHistoryDao.selectList(wrapper);
                list.forEach(System.out::println);
                break;
            }
            case 2:{
                System.out.println("执行水费充值历史的查询");
                QueryWrapper<RisingPricesHistory> wrapper = new QueryWrapper<>();
                wrapper.eq("id","1");
                wrapper.ne("electric","null");
                list = risingPricesHistoryDao.selectList(wrapper);
                list.forEach(System.out::println);
                break;
            }
            case 3:{
                System.out.println("执行水费充值历史的查询");
                QueryWrapper<RisingPricesHistory> wrapper = new QueryWrapper<>();
                wrapper.eq("id","1");
                wrapper.ne("gas","null");
                list = risingPricesHistoryDao.selectList(wrapper);
                list.forEach(System.out::println);
                break;
            }
            default:{

                System.out.println("执行了default");
                System.out.println("哥们别输错了");
            }

        }
        return list;

    }

}
